from django.db import models
from django.contrib.auth.models import User
from asociacion.settings import appdir
from django.core.files.storage import FileSystemStorage
fs = FileSystemStorage(location= appdir)

class Clasificador(models.Model):
    nombre = models.CharField(max_length=63, unique=True)
    creado = models.DateTimeField(auto_now_add=True, editable=False)
    descripcion = models.TextField(blank=True)

    def __unicode__(self):
        return self.nombre


class Tema(models.Model):
    nombre = models.CharField(max_length=63)
    creado = models.DateTimeField(auto_now_add=True, editable=False)
    descripcion = models.TextField(blank=True)
    clasificador = models.ForeignKey(Clasificador)
    creador = models.ForeignKey(User)

    def __unicode__(self):
        return self.nombre



class Documento(models.Model):
    nombre = models.CharField(max_length=63)
    creado = models.DateTimeField(auto_now_add=True, editable=False)
    descripcion = models.TextField(blank=True)
    tema = models.ForeignKey(Tema)
    creador = models.ForeignKey(User, related_name=u"Creador")
    fichero = models.FileField(storage=fs,upload_to='files')
    masuno = models.ManyToManyField(User, related_name=u"MasUno", blank=True, null=True)

    def __unicode__(self):
        return self.nombre


class Comentario(models.Model):
    user = models.ForeignKey(User)
    documento = models.ForeignKey(Documento)
    creado = models.DateTimeField(auto_now_add=True, editable=False)
    texto = models.TextField()
    
    def __unicode__(self):
        return self.user.username + ": "+ self.texto[:80]


class Aviso(models.Model):
    creador = models.ForeignKey(User)
    documento = models.ForeignKey(Documento)
    creado = models.DateTimeField(auto_now_add=True, editable=False)


class SolicitudAcceso(models.Model):
    email = models.EmailField(verbose_name=u'Email de contacto', max_length=127, unique=True)
    nombre = models.CharField(max_length=63, unique=True, blank=True, null=True)
    password = models.CharField(max_length=127)
    token = models.CharField(max_length=16, unique=True)

